#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10, INF = 0x3f3f3f3f;
typedef long long ll;
ll w[maxn], vis[maxn];
vector<ll> G[maxn];
long long N, P, maxa = -INF;

void dfs(ll u, long long ans){
    if(vis[u]) return;
    vis[u] = 1;
    ans += w[u];
    for (auto v:G[u]) if(!vis[v]){
        dfs(v, ans);
    }
    if(ans > maxa) P = u, maxa = ans;
}



int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    cin >> N;
    for (ll i = 1; i <= N; ++i) {
        cin >> w[i];
    }
    for (ll i = 0; i < N - 1; ++i) {
        ll a, b; cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    dfs(1, 0);
    maxa = -INF;
    memset(vis, 0, sizeof(vis));
    dfs(P, 0);
    cout << maxa;
    return 0;
}